#include "ListenThread.h"
#include "Log.h"

CListenThread::CListenThread(CThreadPool *pool) : m_threadPool(pool)
{
	m_socket = NULL;
	m_socket = new CSocket();
}

CListenThread::~CListenThread()
{
	if(m_socket)
	{
		delete m_socket;
		m_socket = NULL;
	}
}

void CListenThread::Run()
{
	pause();
	debug_output("Listen thread is starting ....\n");
	int nSocket; 
	while(1)
	{
		if(m_socket->Listen(nSocket))
		{
		//	debug_output("new client is connecting ..%d..\n", nSocket);
			m_threadPool->pushSocket(nSocket);   ///填加到套接口队列
			m_threadPool->continues();   ///通知线程池，运行起来，到套接口队列中取套接口
		}
		else
		{
			usleep(500);
		}
	}
}

bool CListenThread::Bind(unsigned int nPort)
{
	if(!m_socket->Bind(nPort))
	{
		debug_output("Error Bind port %d is faild\n" , nPort);
		return false;
	}
	debug_output("Success Bind port %d is Successful\n" , nPort);

	continues();

	return true;
}
